#include "raycastcontroller.h"

RayCastController::RayCastController(QObject *parent) : QObject(parent)
{
    p1Area=false;
    p2Area=false;
}

float RayCastController::ReportFixture(b2Fixture* fixture, const b2Vec2& point,const b2Vec2& normal, float fraction){
    QString name=QString(fixture->GetBody()->GetName().c_str());
            qDebug()<<"ray:"<<name<<"frac:"<<fraction<<point.x<<","<<point.y<<normal.x<<normal.y<<endl;

    if(name=="p1"){
        if(fraction<=0.1){
//            qDebug()<<name<<" 进入攻击范围"<<endl;
            p2Area=true;
        }else{
            qDebug()<<name<<"脱离攻击范围"<<endl;
            p2Area=false;
        }
        if(point.y>-42){
            qDebug()<<name<<"脱离攻击范围"<<endl;
            p2Area=false;
        }
        
    }else if(name=="p2"){
        if(fraction<=0.1){
//            qDebug()<<name<<" 进入攻击范围"<< point.y<<endl;
            p1Area=true;
        }else{
            qDebug()<<name<<"脱离攻击范围"<<endl;
            p1Area=false;
        }
        if(point.y>-42){
            qDebug()<<name<<"脱离攻击范围"<<endl;
            p1Area=false;
        }
    }

    return fraction;
}

bool RayCastController::area(QString name){
    if(name=="p1"){
        return p1Area;
    }else if(name=="p2"){
        return p2Area;
    }
}
